package com.smart.campus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.campus.dao.ShClassMapper;
import com.smart.campus.dto.ShClass;
import com.smart.campus.pojo.PagedResponse;
import com.smart.campus.service.ShClassService;
import com.smart.campus.util.PaginationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @BelongsProject: smart-campus
 * @BelongsPackage: com.smart.campus.service.impl
 * @Author: JinBao shan
 * @date 2024/9/23 20:34
 * @Description: TODO
 * @Version: 1.0
 */

@Service
public class ShClassServiceImpl implements ShClassService {
    @Autowired
    private ShClassMapper classMapper;

    @Override
    public PagedResponse<ShClass> findShClassPagination(ShClass shClass, Integer pageNum, Integer pageSize) {
        Page<ShClass> page = new Page<>(pageNum, pageSize);
        QueryWrapper<ShClass> wrapper = new QueryWrapper<>();
        if (shClass.getClassName() != null && !shClass.getClassName().isEmpty()){
            wrapper.like("class_name",shClass.getClassName());
        }
        if (shClass.getGrade() != null){
            wrapper.eq("grade",shClass.getGrade());
        }
        Page<ShClass> classPage = classMapper.selectPage(page, wrapper);
        return PaginationUtil.getPaginationResponse(classPage.getRecords(),pageNum,pageSize,classPage.getTotal());
    }

    @Override
    public List<ShClass> getShClassList(Integer grade) {
        return classMapper.selectList(new QueryWrapper<ShClass>().eq("grade",grade));
    }

    @Override
    public void addShClass(ShClass shClass) {
        if (!shClass.getClassName().isEmpty()){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = sdf.format(new java.util.Date(System.currentTimeMillis()));
            shClass.setCreateTime(format);
            shClass.setUpdateTime(format);
            classMapper.insert(shClass);
        }else {
            throw new RuntimeException();
        }
    }

    @Override
    public ShClass getShClassById(Integer id) {
        return classMapper.selectById(id);
    }

    @Override
    public void updateShClassById(ShClass shClass) {
        if (!shClass.getClassName().isEmpty()){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = sdf.format(new java.util.Date(System.currentTimeMillis()));
            shClass.setUpdateTime(format);
            classMapper.updateById(shClass);
        }else {
            throw new RuntimeException();
        }
    }

    @Override
    public void deleteShClassById(Integer id) {
        classMapper.deleteById(id);
    }

    @Override
    public List<ShClass> getShClassListAll() {
        return classMapper.selectList(null);
    }
}
